﻿<%@ Page Title="" Language="C#" MasterPageFile="~/PropertyInfo.master" AutoEventWireup="true" CodeFile="distance.aspx.cs" Inherits="Property_Info_Distance" %>
<%@ MasterType VirtualPath="~/PropertyInfo.master" %>
<asp:Content ID="ContentHeader" runat="server" ContentPlaceHolderID="HeaderContentPlaceHolder">
    <title>Google Maps JavaScript API Example: Simple Map</title>
</asp:Content>

<asp:Content ID="Content1" ContentPlaceHolderID="SidebarContentPlaceHolder" Runat="Server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" Runat="Server">
    <div id="map" style="width: 600px; height: 400px; display: none;"></div>
	<div id="searchcontrol" style="width: 0px; height: 0px; display: none;"></div>
	<asp:Button ID="store" runat="server" OnClick="setNearestAmenity" Text="store" style="display: none;"/>  
    
    <asp:Literal ID="Hid" runat="server"/>    
    <script src="http://maps.google.com/maps?file=api&amp;v=2.x&amp;sensor=false&amp;key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA"
        type="text/javascript"></script>
    <script src="http://www.google.com/uds/api?file=uds.js&amp;v=1.0" type="text/javascript"></script>
    <script src="http://www.google.com/uds/solutions/localsearch/gmlocalsearch.js" type="text/javascript"></script>
    <script type="text/javascript">
        var map = null;
        var zoom = 13;
   
        var searchControl = null;
        var localSearch = null;

        var propertyLoc = null;

        /*
        property_address, property_lat, property_lng, amenities, 
        minDistanceToAmenity, dist_element, and store_btn are declared in cs.
        */
    </script>
    <asp:Literal ID="JS" runat="server"/>  
    <script type="text/javascript">
        function load() {
            if (GBrowserIsCompatible()) {
                map = new GMap2(document.getElementById("map"));

                var geocoder = new GClientGeocoder();
                geocoder.setBaseCountryCode("us");

                if (geocoder) {
                    geocoder.getLatLng(property_address, function(point)
                    {
                        if (!point) {
                            return;
                        }
                        else {
                            map.setCenter(point, zoom);
                            
                            getFindNearest(property_address);      
                        }
                    });
                }
            }
            else {
                alert("Browser not compatible");
            }
        }

        function getFindNearest(property_address)
        {
            searchControl = new google.search.SearchControl();
            localSearch = new google.search.LocalSearch();
            var options = new google.search.SearcherOptions();

            options.setExpandMode(GSearchControl.EXPAND_MODE_CLOSED);
            searchControl.addSearcher(localSearch, options);
            localSearch.setCenterPoint(map);
            searchControl.draw(property_address);
            searchControl.setSearchCompleteCallback(null, function() {
                if (localSearch.results.length >= 1) {
                    propertyLoc = localSearch.results[0];
                    property_lat.value = propertyLoc.lat;
                    property_lng.value = propertyLoc.lng;

                    calcMinDistanceToAmenity();
                }
            });
            searchControl.execute(property_address);
        }
        function calcMinDistanceToAmenity() {

            getMinDistanceToAmenity(0);
        }

        function getMinDistanceToAmenity(index) {
            if (index < amenities.length) {
                var amenitySearchControl = new google.search.SearchControl();
                var amenitySearch = new google.search.LocalSearch({ resultList: google.maps.LocalSearch.RESULT_LIST_SUPPRESS });
                amenitySearchControl.addSearcher(amenitySearch);
                amenitySearch.setCenterPoint(map);
                amenitySearchControl.draw(document.getElementById("searchcontrol"));
                amenitySearchControl.setSearchCompleteCallback(null, function() {
      
                    getMinDistance(amenitySearch.results, 0, 0, index);

                });
                amenitySearchControl.execute(amenities[index]);
            }
            else {
                for (var i = 0; i < amenities.length; i++) {

                    document.getElementById(dist_element[i]).value = minDistanceToAmenity[i]; 
                }
                //alert("done")
                store_btn.click();
            }
        }

        function getMinDistance(results, min_distance, result_index, index) {
            if (result_index < results.length) {
                var dir = new GDirections();
                var result = results[result_index];
                GEvent.addListener(dir, "load", function() {
                    var distance = dir.getDistance().meters / 1609.344;
                    if (min_distance == 0 || distance < min_distance) {
                        min_distance = distance;
                    }
                    getMinDistance(results, min_distance, result_index + 1, index);
                });
                dir.load("from: " + propertyLoc.lat + "," + propertyLoc.lng + " to: " + result.lat + "," + result.lng);    
             }
             else {
                minDistanceToAmenity[index] = min_distance;
                //alert(amenities[index] + ": " + min_distance);
                getMinDistanceToAmenity(index+1);
            }
        }
        var browser = navigator.appName;
        if (browser == "Microsoft Internet Explorer") {
            document.body.onload = load;
        }
        else {
            document.body.onload = load();
        }
       
    </script>	
</asp:Content>
